package sdc.media.motorbike.Model;

import sdc.media.motorbike.Controller.Assets;
import sdc.media.motorbike.Controller.Constant;
import sdc.media.motorbike.Controller.WorldGame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;

public class Motor extends Object {

	private float sizeX, sizeY;
	private int type;
	private float speed;
	
	public enum MoveState {
		MOVE_LEFT,
		MOVE_RIGHT,
		IDLE
	}
	
	public MoveState moveState;
	public long startTime;
	public long endTime;
	
	public void init(World world, String name, Vector2 position, int type) {
		super.init(world, name, position);
		// TODO Auto-generated constructor stub
		this.type = type;
		switch (type) {
		case 1:
			setImage(Assets.car1);
			break;
		case 2:
			setImage(Assets.car2);
			break;
		case 3:
			setImage(Assets.car3);
			break;
		default:
			break;
		}
		setSprite(new Sprite(getImage()));
		setBody(world.createBody(getBodyDef()));
		getSprite().setScale(1, 1);
		scaleBody();
		getBody().setFixedRotation(true);
		loadSpriteToBody();
		
		moveState = MoveState.IDLE;
	}
	
	// Update
	public void update(WorldGame world) {
		Vector2 vel = getBody().getLinearVelocity();
		switch (moveState) {
		case MOVE_LEFT: //desiredVel = max(vel.x - 0.1f, -5.0f);
			vel.x = -25.0f * world.sizeX;
			break;
		case IDLE: //desiredVel *= 0.98f;
			vel.x = 0f;
			break;
		case MOVE_RIGHT: //desiredVel = min(vel.x + 0.1f, 5.0f);
			vel.x = 25.0f * world.sizeX;
			break;
		default:
			break;
		}
		if(vel.y < 0)
			vel.y = 0;
		
		//getBody().setLinearVelocity(vel);
		//getBody().setAngularVelocity(vel.x / 3 * Constant.DEGTORAD);
		
		getBody().setAngularVelocity(-vel.x / 3 * Gdx.input.getAccelerometerX() * Constant.DEGTORAD);
		getBody().setLinearVelocity(20 * Gdx.input.getAccelerometerX(), 0);
		
		// Scale Body
		for (int i = 0; i < getBody().getFixtureList().size(); i++) {
			getBody().destroyFixture(getBody().getFixtureList().get(i));
		}
		scaleBody();
		
	}

	public int getType() {
		return type;
	}
	
	public void setType(int type) {
		this.type = type;
	}
	
	public float getSpeed() {
		return speed;
	}
	
	public void setSpeed(float speed) {
		this.speed = speed;
	}
	
	public void restartTime() {
		startTime = endTime = 0;
	}
	
	public float min(float f, float g) {
		// TODO Auto-generated method stub
		return f < g ? f : g;
	}

	public float max(float f, float g) {
		// TODO Auto-generated method stub
		return f > g ? f : g;
	}

	public void scaleBody() {
		// TODO Auto-generated method stub
		// scale body
		sizeX = getSprite().getScaleX();
		sizeY = getSprite().getScaleY();
		
		PolygonShape newShape = new PolygonShape();
		newShape.setAsBox((getWidth()-10) / (2.0f * Constant.PIXEL_PER_METER) * sizeX, (getHeight()-10) / (2.0f * Constant.PIXEL_PER_METER) * sizeY);
		getFixtureDef().shape = newShape;
		getFixtureDef().restitution = 0.1f;
		getBody().createFixture(getFixtureDef());
		getBody().getFixtureList().get(0).setDensity(50);
		getBody().resetMassData();
		newShape.dispose();
	}
}
